﻿using System;
using System.Collections.Generic;
using System.Text;
using Abp.Application.Services;
using Abp.AutoMapper;
using System.Linq;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.Domain.Repositories;
using Sunlit.Business.HomeSliders.Dto;
using Sunlit.Business.IO;
using Abp.Application.Services.Dto;
using System.Threading.Tasks;
using System.IO;

namespace Sunlit.Business.HomeSliders
{
    public class HomeSliderAppService: AsyncCrudAppService<HomeSlider,HomeSliderDto,int,GetAllHomeSliderInput
        ,CreateHomeSliderInput,UpdateHomeSliderInput>,IHomeSliderAppService
    {
        private readonly IAppFolders _appFolders;
        public HomeSliderAppService(IRepository<HomeSlider> repository,
            IAppFolders appFolders) : base(repository)
        {
            _appFolders = appFolders;
        }
        public override async Task DeleteAsync(EntityDto<int> input)
        {
            var deleteEntity = await this.GetAsync(input);
            if (!string.IsNullOrWhiteSpace(deleteEntity.ImageUrl))
            {
                var fileNameWithoutExt = Path.GetFileNameWithoutExtension(deleteEntity.ImageUrl);
                var pictureType = fileNameWithoutExt.Split("_").First();
                var userHonorPath = Path.Combine(_appFolders.PictureUpladFolder, pictureType);
                AppFileHelper.DeleteFilesInFolderIfExists(userHonorPath, fileNameWithoutExt);
            }
            await base.DeleteAsync(input);
        }
    }
}
